package com.zycfc.zsf.boot.common.yaml;

import com.zycfc.zsf.boot.util.text.*;
import org.yaml.snakeyaml.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.slf4j.*;

public class YamlReader
{
    private static final Logger log;
    private static Map<String, Map<String, Object>> properties;
    public static final YamlReader instance;
    
    public <T> T get(final String yaml, final String key) {
        return (T)this.getObject(yaml, key);
    }
    
    public Object getObject(final String yaml, final String key) {
        if (StringUtils.isEmpty(yaml)) {
            return null;
        }
        final Yaml yamlObj = new Yaml();
        final Map<String, Map<String, Object>> data = (Map<String, Map<String, Object>>)yamlObj.loadAs(yaml, (Class)HashMap.class);
        if (data == null || data.isEmpty()) {
            return null;
        }
        final String separator = ".";
        String[] separatorKeys = null;
        if (key.contains(separator)) {
            separatorKeys = key.split("\\.");
            Map<String, Object> finalValue = new HashMap<>();
            for (int i = 0; i < separatorKeys.length - 1; ++i) {
                if (i == 0) {
                    finalValue = data.get(separatorKeys[i]);
                }
                else {
                    if (finalValue == null) {
                        break;
                    }
                    finalValue = (Map<String, Object>) finalValue.get(separatorKeys[i]);
                }
            }
            return (finalValue == null) ? null : finalValue.get(separatorKeys[separatorKeys.length - 1]);
        }
        return data.get(key);
    }
    
    public Object getValueByKey(final String fileName, final String key) {
        final Yaml yaml = new Yaml();
        try (final InputStream in = YamlReader.class.getClassLoader().getResourceAsStream(fileName)) {
            YamlReader.properties = (Map<String, Map<String, Object>>)yaml.loadAs(in, (Class)HashMap.class);
        }
        catch (Exception e) {
            YamlReader.log.error("Init yaml failed !", (Throwable)e);
        }
        final String separator = ".";
        String[] separatorKeys = null;
        if (key.contains(separator)) {
            separatorKeys = key.split("\\.");
            Map<String, Object> finalValue = new HashMap<>();
            for (int i = 0; i < separatorKeys.length - 1; ++i) {
                if (i == 0) {
                    finalValue = YamlReader.properties.get(separatorKeys[i]);
                }
                else {
                    if (finalValue == null) {
                        break;
                    }
                    finalValue = (Map<String, Object>) finalValue.get(separatorKeys[i]);
                }
            }
            return (finalValue == null) ? null : finalValue.get(separatorKeys[separatorKeys.length - 1]);
        }
        return YamlReader.properties.get(key);
    }
    
    public static List<String> prefixValue(final String value) {
        return getFields("$", value);
    }
    
    private static List<String> getFields(final String prefix, final String sql) {
        final List<String> list = new ArrayList<String>();
        final Pattern regex = Pattern.compile("\\" + prefix + "\\{([^}]*)\\}");
        final Matcher matcher = regex.matcher(sql);
        while (matcher.find()) {
            list.add(matcher.group(1));
        }
        return list;
    }
    
    public static void main(final String[] args) {
        final YamlReader reader = new YamlReader();
        final String yaml = "myown:\r\n  test: myow-111\r\n\r\na1:\r\n  b1:\r\n    c1: 3000";
        final Integer a = reader.get(yaml, "a1.b1.c1");
        System.out.println(a);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)YamlReader.class);
        YamlReader.properties = new HashMap<String, Map<String, Object>>();
        instance = new YamlReader();
    }
}
